<?php
namespace Ader\Controller;

class PlanController extends Controller {

    public function index() {
        $m    = D2();
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        $this->assign('page', $page)->display();
    }

    /**
     * doPlay
     * 开启计划
     *
     * @return void
     */
    public function doPlay() {
        $id = I('get.id', 0, 'intval');

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整'));
        }

        try {
            $r = D2()->play($id)
                 ? array('s' => true, 'm' => '计划开启成功')
                 : array('s' => false, 'm' => '计划开启失败');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

    /**
     * doStop
     * 停止计划
     *
     * @return void
     */
    public function doStop() {
        $id = I('get.id', 0);

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整'));
        }

        $m = D2();

        try {
            $r = D2()->stop($id)
                 ? array('s' => true, 'm' => '计划已停止')
                 : array('s' => false, 'm' => '计划停止失败');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

}
